<?php

namespace  App\Models;

use App\Models\Forms\Template;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;


/**
 * This is the model class for table "circle".
 *
 * @property int $id
 * @property string $title 标题
 * @property string $shop 商城
 * @property int $status 发送状态
 * @property string $wx_id 发送微信
 * @property int $agent 所属代理
 * @property string $created_at 加入时间
 * @property string $send_time 预估发送时间
 * @property int $user_id
 * @property string $raw_data 保存的元对象
 * @property string $cover 图片
 */
class Circle extends Model implements IReadable
{


    protected $table = "circle";
    protected $guarded = [];

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => '标题',
            'shop' => '商城',
            'status' => [
                'label' => '发送状态',
                'type' => 'text',
                'map' => [
                    '未发送', '已发送'
                ]
            ],
            //'wx_id' => '发送微信',
            //'agent' => '所属代理',
            'in_queue' => [
                'label' => '加入队列',
                'width' => '50',
                'type' => "text",
                'map' => [
                    "否",
                    "是"
                ]
            ],
            'article'=> '文案',
            'send_time' => [
                'type' => 'text',
                'label' => '预估发送时间',
                'callable' => function($row)  {
                    if ($row != "1970-01-01 00:00:00") {
                        return $row;
                    }
                    return date("Y-m-d H:i:s", strtotime("+5 minute"));
                }
            ],
            'created_at' => [
                'type' => 'text',
                'label' => '采集时间',
                'callable' => function($row) {
                    return date("Y-m-d H:i:s", strtotime($row));
                }
            ],
        ];
    }

    public static function saveData(Template $template, $uid)
    {
        if (!$template || is_null($template)) {
            return false;
        }
        $content = $template->getStringByTemplate(true);
        $model = new self();
        $model->title = $template->title;
        $model->content = htmlspecialchars($content);
        $model->article = $template->article;   //文案
        $model->group_id = "1";
        $model->send_time = "1970-01-01 00:00:00";
        $model->user_id = $uid;
        $model->raw_data = $template->toJson();
        $model->images = \GuzzleHttp\json_encode($template->getImages());
        $model->shop = $template->shop;
        $model->save();
        return true;
    }

    public static function addQueue( $messageId)
    {
        $message = self::query()->where('id', $messageId)->first();
        if ($message->in_queue == 1) {
            return true;
        }
        DB::transaction(function() use ($message) {
            $queue = new Queue();
            $queue->is_read = 0;
            $queue->message_id = $message->id;
            $queue->user_id = $message->user_id;
            $queue->type = Queue::send_type_circle;
            $queue->save();
            $message->in_queue = 1;
            $message->queue_id = $queue->id;
            $message->save();
            return $queue->save();
        });
    }
}
